package com.xiaoer.studyenglishdemo.ui;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.xiaoer.studyenglishdemo.R;
import com.xiaoer.studyenglishdemo.bean.WordBean;
import com.xiaoer.studyenglishdemo.util.WordsDBDao;
import com.xiaoer.studyenglishdemo.util.WordsLoader;

import java.util.ArrayList;
import java.util.List;

public class AddWordActivity extends AppCompatActivity {

    Button btn_add;
    RecyclerView rv_list;

    RvAdapter adapter;

    List<WordBean> wordBeans = new ArrayList<>();

    public static void startMe(Context context) {
        Intent intent = new Intent(context, AddWordActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_word);

        btn_add = findViewById(R.id.btn_add);
        rv_list = findViewById(R.id.rv_list);

        adapter = new RvAdapter();
        rv_list.setAdapter(adapter);
        rv_list.setLayoutManager(new LinearLayoutManager(this));

        btn_add.setOnClickListener(v -> {
            EditWordsActivity.startMe(this, -1);
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        refreshData();
    }

    private void refreshData() {
        wordBeans.clear();
        wordBeans.addAll(WordsLoader.getAllWords(this));
        adapter.notifyDataSetChanged();
    }

    public class RvAdapter extends RecyclerView.Adapter<RvAdapter.ViewHolder> {
        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(AddWordActivity.this).inflate(R.layout.item_add_word_activity, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            holder.tv_words.setText(wordBeans.get(position).getIntroduce());
            holder.tv_means.setText(wordBeans.get(position).getMeans());
            holder.btn_edit.setOnClickListener(v -> {
                EditWordsActivity.startMe(AddWordActivity.this, wordBeans.get(position).getId());
            });
            holder.btn_del.setOnClickListener(v -> {
                new AlertDialog.Builder(AddWordActivity.this)
                        .setMessage("确定要删除这个单词吗？")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                boolean delete = WordsDBDao.delete(AddWordActivity.this, wordBeans.get(position).getId());
                                if (delete) {
                                    refreshData();
                                }
                                dialog.dismiss();
                            }
                        })
                        .setNegativeButton("取消", null)
                        .show();
            });
        }

        @Override
        public int getItemCount() {
            return wordBeans.size();
        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            Button btn_del;
            Button btn_edit;
            TextView tv_words;
            TextView tv_means;

            public ViewHolder(@NonNull View itemView) {
                super(itemView);
                btn_del = itemView.findViewById(R.id.btn_del);
                btn_edit = itemView.findViewById(R.id.btn_edit);
                tv_words = itemView.findViewById(R.id.tv_words);
                tv_means = itemView.findViewById(R.id.tv_means);
            }
        }
    }
}